<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/6/14
 * Time: 1:17 PM
 */
namespace Admin\Controller;
use Admin\Model\AdminTopicImg;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Velacolib\Utility\Utility;

class TopicImgController extends AbstractActionController
{
    protected  $TopicTable;
    protected  $TopicImgTable;
    protected $sm;
    protected $service_locator_str;
    protected $auth_obj;
    public function onDispatch(\Zend\Mvc\MvcEvent $e)
    {
        $this->service_locator_str = 'Admin\Model\AdminTopicTable';
        $this->sm = $this->getServiceLocator();
        $this->TopicTable = $this->sm->get($this->service_locator_str);


        $this->service_locator_str = 'Admin\Model\AdminTopicImgTable';
        $this->sm = $this->getServiceLocator();
        $this->TopicImgTable = $this->sm->get($this->service_locator_str);

        $this->auth = Utility::CheckLogin($this);
        if ($this->auth!=null && $this->auth->user_type != 3 ) {
            $this->redirect()->toRoute('admin/child',array('controller'=>'login'));
        }
        if ($this->auth==null){
            $this->redirect()->toRoute('admin/child',array('controller'=>'login'));
        }
        return parent::onDispatch($e);
    }

    public function init(){
        //$this->Acl->SetModule('Project');
        //if(!$this->Acl->Check())
        //  $this->redirect()->toRoute('home', array ('action' => 'index'));
    }

    public function indexAction()
    {

        $arr_topic = $this->TopicTable->fetchAll();
        $user_id = $this->auth_obj->userid;

        return new ViewModel(array('topic_array' => $arr_topic, 'user_id' => $user_id));
    }

    public function addAction(){
        $file = $this->params()->fromFiles();
        $post = $this->params()->fromPost();
        $file_name = $file['file']['name'];
        $file_tmp = $file['file']['tmp_name'];
        $file_size = $file['file']['size'];
        $file_error = $file['file']['error'];
        $file_type = $file['file']['type'];
        move_uploaded_file($file_tmp, "./public/img/upload/".$file_name);

        //store file name in db;
        $user_id = $post['user_id'];
        $topic_id = $post['topic_id'];

        $data = array(
            'link'=>'/img/upload/'.$file_name,
            'topic_id' => $topic_id,
            'user_id' => $user_id,
            'img_desc' => ''
        );
        $topicImgModel = new AdminTopicImg();
        $topicImgModel->exchangeArray($data);
        $tof = $this->TopicImgTable->saveTopicImg($topicImgModel);
    }
    public function deleteAction(){


    }
    public function editAction(){


    }
    public function detailAction(){

    }

    function deleteImageAction(){
        $id = $this->params()->fromPost('id');
        $src =$this->params()->fromPost('src');

        if (!$id) {
            $response['result'] = false;
            $response['message'] = 'ID invalid, please try again!';
        } else {
            $this->TopicImgTable->deleteTopicImg($id);
            unlink('./public'.$src)  ;
            $response['result'] = true;
            $response['message'] = 'Success!';
        }
        echo json_encode($response);die;

    }
}